﻿<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="YearHz.aspx.cs" Inherits="Thksoft.Module.ApplyView.Apply.Edit.YearHz" %>

<!DOCTYPE html>
<html class="L_UI">
<head>
    <meta charset="utf-8" />
    <title>资金集中管理平台</title>
    <link href="/CSS/Main.css" rel="stylesheet" />
    <link href="/App_Themes/MSN_Blue/default.css" rel="stylesheet" type="text/css" />
    <link href="/UserCotrols/My97DatePicker/skin/WdatePicker.css" rel="stylesheet" />
    <style type="text/css">
        .Apply-Row-Lev-1 {
            background-color: #B2D8FF;
            font-size: 14px;
        }

        .Apply-Row-Lev-2 {
            background-color: #CCCCCC;
            font-size: 13px;
        }

        .Apply-Row-Lev-3 {
            background-color: white;
            font-size: 12px;
        }

        .Apply-Row-Lev-1 .INPUT {
            background-color: #B2D8FF;
            border-color: white;
        }

        .Apply-Row-Lev-2 .INPUT {
            background-color: #CCCCCC;
            border-color: white;
        }

        .Apply-Row-Lev-3 .INPUT {
            border-color: white;
        }
    </style>
</head>
<body>
    <div class="Window">
        <div id="app" class="Content" style="top: 0px;">
            <div class="Header lbltitle">
                年资金计划
            </div>
            <div class="Content">
                <div class="Content" style="overflow-x: auto; overflow-y: auto; top: 0px; bottom: 40px;">
                    <table class="table" width="100%" border="0" cellpadding="0" cellspacing="0">
                        <tr style="background-color:#29a1f7;opacity:0.7; font-size:16px;">
                            <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">编号</th>
                            <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">项目名称</th>
                            <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">申请金额</th>
                            <th style="border: 1px solid #BAD4E1;text-align:center;color:white;">备注</th>
                        </tr>
                        <template v-for="item in Models">
                    <tr :class="GetLev2(item)">
                        <td style="text-align:center;">{{item.N_PROJECT_ID}}</td>
                        <td>{{item.C_PROJECT_NAME}}</td>
                        <template v-if="item.N_TYPE==2 || item.N_TYPE == 3">
                            <td style="padding-right:8px;"><input class="INPUT" style="text-align:right; width:85px;margin:0;padding:0;" :value="item.N_MONEY" @change="MoneyChange($event,item);" type="text" /></td>
                            <td style="padding-right:8px;"><input class="INPUT" style="width:100%;margin:0;padding:0;" v-model="item.C_REMARK" type="text" /></td>
                        </template>
                        <template v-else>
                            <td style="padding-right:8px;text-align:right;">{{item.N_MONEY}}</td>
                             <td style="padding-right:8px;"><input class="INPUT" style="width:100%;margin:0;padding:0;" v-model="item.C_REMARK" type="text" /></td>
                        </template>
                    </tr>
                </template>
                    </table>
                </div>
                <div class="Header" style="height: 40px; bottom: 0px; top: initial; text-align: center;">
                    <button type="button" class="bt_css_s" @click="Save">保存</button>
                    <button type="button" class="bt_css_s" @click="Load">初始化</button>
                </div>
            </div>
        </div>
    </div>
    <script src="/Scripts/jquery.1.11.1.js" type="text/javascript"></script>
    <script src="/UserCotrols/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
    <script src="/UserCotrols/My97DatePicker/开发包/lang/zh-cn.js" type="text/javascript"></script>
    <script src="/Content/lib/jslinq/linq.js" type="text/javascript"></script>
    <script src="/Content/lib/vue/vue-2.5.17.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        //构造Web程序入口
        var app = new Vue({
            el: "#app",
            data: {
                Models: [],
                ApplyId:<%=Request.Params.Get("ApplyId") %>
            },
            methods: {
                Close: function () {
                    if (top.layer == undefined) {
                        window.close();
                        return;
                    }
                    var index = top.layer.getFrameIndex(window.name); //获取窗口索引
                    try {
                        top.$("#layui-layer" + index + " .layui-layer-close").click();
                    } catch (e) {
                        console.log("已强制关闭页面!");
                        //强制关闭页面
                        try {
                            top.layer.close(index);
                        } catch (e) {
                            console.log("强制关闭页面异常!");
                        }
                    };
                },
                Save: function () {
                    var that = this;
                    var lodding = top.layer.load(1, {
                        shade: [0.8, '#393D49'],
                        content: '等待服务器端响应...',
                        success: function (layero) {
                            layero.find('.layui-layer-content').css({
                                'padding-top': '39px',
                                'width': '120px'
                            });
                        }
                    });
                    var data = [];
                    var count = 0;
                    for (var i = 0; i < that.Models.length; i++) {
                        data.push({
                            ProjectId: that.Models[i].N_PROJECT_ID,
                            Money: that.Models[i].N_MONEY == null ? 0 : that.Models[i].N_MONEY,
                            Remark: that.Models[i].C_REMARK == null ? "" : that.Models[i].C_REMARK
                        });
                        if (that.Models[i].N_MONEY == null || that.Models[i].N_MONEY == 0) {
                            count++;
                        }
                    }
                    if (count == that.Models.length) {
                        top.layer.close(lodding);
                        if (confirm("你没有填写资金计划,是否继续填写?")) {
                            return;
                        }
                    }
                    //获取信息
                    $.ajax({
                        type: "POST",
                        url: "/Module/ApplyView/ApplyHandler.ashx?MethodType=Save",
                        data: JSON.stringify({
                            Type: "Year",
                            ApplyId: that.ApplyId,
                            Data: data
                        }),
                        success: function (result) {
                            if (result.Type == 1) {
                                if (confirm("提交成功，是否关闭当前页面")) {
                                    that.Close();
                                }
                            }
                            top.layer.close(lodding);
                        },
                        error: function () {
                            alert("网络断开或服务异常");
                            top.layer.close(lodding);
                        }
                    });
                },
                GetLev: function (item) {
                    var lev = 1;
                    var that = this;
                    _GetLev(item);
                    switch (lev % 3) {
                        case 0:
                            return "Apply-Row-Lev-3";
                        case 1:
                            return "Apply-Row-Lev-1";
                        case 2:
                            return "Apply-Row-Lev-2";
                        default:
                            return "Apply-Row-Lev-2";
                    }

                    /**
                     * 递归获取等级
                     * @@param item 当前对象
                     * @@returns void
                     */
                    function _GetLev(item) {
                        if (item.N_PARENT_PROJECT_ID == 0 || item.N_PARENT_PROJECT_ID == null) {
                            return;
                        }
                        item = _GetByPid(item);
                        if (item == null) {
                            return;
                        }
                        lev += 1;
                        _GetLev(item);
                    }
                    /**
                     * 通过父级Id获取父级对象
                     * @@param item 当前对象
                     * @@returns Object 父级对象
                     */
                    function _GetByPid(item) {
                        for (var i = 0; i < that.Models.length; i++) {
                            if (that.Models[i].N_PROJECT_ID == item.N_PARENT_PROJECT_ID) {
                                return that.Models[i];
                            }
                        }
                        return null;
                    }
                },
                GetLev2: function (item) {
                    if (item.N_PARENT_PROJECT_ID == null || item.N_PARENT_PROJECT_ID == 0) {
                        return "Apply-Row-Lev-1";
                    }
                    if (item.N_TYPE == 1) {
                        return "Apply-Row-Lev-2";
                    }
                    return "Apply-Row-Lev-3";
                },
                MoneyChange: function (event, item) {
                    var that = this;
                    if (event == null) {
                        var money = UpdateMoney(null);
                        return money;
                    }

                    if (item.N_TYPE == 2 || item.N_TYPE == 3) {
                        var value = null;
                        try {
                            if (event.target.value == "") {
                                //return;
                                event.target.value = 0;
                            }
                            value = eval(event.target.value);
                            console.log(value);
                            //value = parseFloat(event.target.value);
                        } catch (e) {
                            value = null;
                        }
                        if (value == null || value == undefined || isNaN(value) || parseFloat(value)>= 2147483647) {
                            if (confirm("请输入正确的金额,是否恢复原有数值？")) {

                                event.target.value = item.N_MONEY.toString();
                            }
                            return;
                        }
                        //console.log(item);
                        item.N_MONEY = parseFloat(value.toFixed(4));
                        //console.log(value);
                        var money = UpdateMoney(0);
                        console.log("总额：" + money);

                    }
                    //重新计算Type

                    /**
                         * 递归更新父级对象
                         * @@param pid 父级Id
                         * @@returns 金额总额
                         */
                    function UpdateMoney(pid) {
                        var arry = GetByPid(pid);
                        for (var i = 0; i < arry.length; i++) {
                            //console.log(arry);
                            if (arry[i].N_TYPE != 2 && arry[i].N_TYPE != 3) {
                                arry[i].N_MONEY = UpdateMoney(arry[i].N_PROJECT_ID);
                            }
                        }
                        var money = 0;
                        for (var i = 0; i < arry.length; i++) {
                            if (arry[i].N_MONEY == null || arry[i].N_TYPE == 3) {
                                continue;
                            }
                            var value = arry[i].N_MONEY;
                            //console.log(value);
                            money += value;
                        }
                        return parseFloat(money.toFixed(4));
                    }
                    /**
                     * 通过Pid 获取页面模型的对象
                     * @@param pid 父级Id
                     * @@ Arry 子级对象集合
                     */
                    function GetByPid(pid) {
                        var arry = [];
                        for (var i = 0; i < that.Models.length; i++) {
                            if (that.Models[i].N_PARENT_PROJECT_ID == pid) {
                                arry.push(that.Models[i]);
                            }
                        }
                        return arry;
                    }
                },
                Load: function () {
                    var that = this;
                    //Ajax调用Load接口
                    $.ajax({
                        async: false,
                        type: "POST",
                        url: "/Module/ApplyView/ApplyHandler.ashx?MethodType=Get",
                        data: JSON.stringify({
                            Type: "YearHzLoad",
                            ApplyId: that.ApplyId
                        }),
                        success: function (result) {
                            if (result.Type == 1) {
                                for (var i = 0; i < result.Data.length; i++) {
                                    for (var k = 0; k < that.Models.length; k++) {
                                        if (that.Models[k].N_PROJECT_ID == result.Data[i].N_PROJECT_ID) {
                                            that.Models[k].N_MONEY = result.Data[i].N_MONEY;
                                            that.Models[k].C_REMARK = result.Data[i].C_REMARK;
                                            break;
                                        }
                                    }
                                }
                                //that.Models = that.BindData(result.Data, 0);
                                that.MoneyChange(null, null);
                            }
                        },
                        error: function () {
                            alert("网络断开或服务异常");
                        }
                    });
                },
                BindData: function (data, pid) {
                    var that = this;
                    var arry = [];
                    var currentData = Enumerable.From(data).Where("a => a.N_PARENT_PROJECT_ID ==" + pid).ToArray();
                    for (var i = 0; i < currentData.length; i++) {
                        arry.push(currentData[i]);
                        var currentData1 = that.BindData(data, currentData[i].N_PROJECT_ID);
                        for (var k = 0; k < currentData1.length; k++) {
                            arry.push(currentData1[k]);
                        }
                    }
                    return arry;
                }
            },
            mounted: function () {
                var that = this;
                //获取信息
                $.ajax({
                    async: false,
                    type: "POST",
                    url: "/Module/ApplyView/ApplyHandler.ashx?MethodType=Get",
                    data: JSON.stringify({
                        Type: "YearHz",
                        ApplyId: that.ApplyId
                    }),
                    success: function (result) {
                        if (result.Type == 1) {
                            //对数据做兼容处理
                            var data = [];
                            for (var i = 0; i < result.Data.length; i++) {
                                var current = result.Data[i];
                                data.push({
                                    N_PROJECT_ID: current.N_PROID,
                                    C_PROJECT_NAME: current.C_PRONAME,
                                    N_PARENT_PROJECT_ID: current.N_PARENTID,
                                    N_TYPE: current.N_TYPE,
                                    N_MONEY: current.N_AMOUNT == null ? 0 : current.N_AMOUNT,
                                    C_REMARK: current.C_REMARK == null ? "" : current.C_REMARK
                                });
                            }
                            //console.log(that.BindData(data, "0"));
                            that.Models = that.BindData(data, "0");
                        }
                    },
                    error: function () {

                        alert("网络断开或服务异常");
                    }
                });
            }
        });
    </script>
</body>
</html>